<script setup lang="ts">
import { onMounted, ref, watch } from 'vue';
import { message } from 'ant-design-vue';
import { getSourceFileUrlApi } from '@/api/study/course';
import VueOfficePdf from '@vue-office/pdf';
const props = defineProps<{
  sourceId: string;
}>();

const sourceFileUrl = ref<string | null>(null)

// 获取具体资源URL
const fetchSourceFileInfo = async (sourceId: string) => {
  try {
    const response = await getSourceFileUrlApi(sourceId);
    sourceFileUrl.value = response;
  }
  catch (error) {
    message.error("获取资源信息失败")
  }
}

watch(() => props.sourceId, (newSourceId: string) => {
  fetchSourceFileInfo(newSourceId)
})

onMounted(() => {
  fetchSourceFileInfo(props.sourceId)
})
</script>

<template>
  <div class="container">
    <div class="navigation">
      <VueOfficePdf
        v-if="sourceFileUrl"
        :src="sourceFileUrl"
        width="100%"
        height="800px"
        style="border: none; margin-bottom: 0px;"
      />
      <p v-else>
        PDF 暂时无法获取
      </p>
    </div>
  </div>
</template>

<style scoped>
.container {
  padding: 20px;
  background-color: #f7f8fc;
  border-radius: 8px;
  margin-bottom: 20px;
  position: relative;
  margin-left: -20px;
  margin-right: -20px;

  }

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 10px;
}
.pdfContainer {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
.headerTitle {
  font-size: 18px;
  font-weight: 0;
  color: #333;
  margin: 0;
}

.addButton {
  background: linear-gradient(135deg, rgba(0, 51, 153, 0.8) 0%, rgba(0, 51, 153, 0.5) 100%);
      box-shadow: 0 4px 10px rgba(0, 51, 153, 0.3);

  color: #fff;
  border-radius: 20px;
  border: none;
  cursor: pointer;
  -webkit-border-radius: 20px;
  -moz-border-radius: 20px;
  -ms-border-radius: 20px;
  -o-border-radius: 20px;
}

.navigation {
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 4px 12px rgba(0.1, 0.1, 0.1, 0.1);
  height: 82vh;
}
.navigation :deep(.vue-office-pdf) {
  height: 100% !important;
}
.title {
  font-size: 28px;
  font-weight: 700;
  color: #000;
  margin-bottom: -20px;
}

.intro, .description, .conclusion, .additionalText {
  font-size: 17px;
  color: #333;
  line-height: 1.8;
  margin-bottom: -50px;
}

.conclusion {
  margin-bottom: -50px;
}

.additionalText {
  margin-bottom: 20px;
}

.signature {
  font-size: 17px;
  color: #333;
  line-height: 1.8;
  text-align: right;
  margin-top: 40px;
}
</style>
